/*
 * frmAddAgenda.java
 *
 * Created on 04/10/2012, 22:01:17
 */
package br.com.petshop.dao.frames;

import br.com.petshop.dao.AgendaDAO;
import br.com.petshop.dao.AnimalDAO;
import br.com.petshop.entidades.Agenda;
import br.com.petshop.entidades.AgendaType;
import br.com.petshop.entidades.Animal;
import java.sql.SQLException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.Vector;
import java.util.logging.Logger;
import javax.swing.DefaultComboBoxModel;
import javax.swing.JOptionPane;

/**
 *
 * @author sidney
 */
public class frmAtualizarAgenda extends javax.swing.JDialog {

    private final AgendaDAO agDAO;
    private final Agenda agenda;

    /** Creates new form frmAddAgenda */
    public frmAtualizarAgenda(java.awt.Frame parent, boolean modal, AgendaDAO agDAO,Agenda agenda) {
        super(parent, modal);
        this.agDAO = agDAO;
        this.agenda = agenda;
        initComponents();
        loadInitialComboData();
        loadAgendamentoInComboBox();
        jComboBoxAnimal.requestFocus();
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        panelComponents = new javax.swing.JPanel();
        jlAnimal = new javax.swing.JLabel();
        jlTipo = new javax.swing.JLabel();
        jlTotal = new javax.swing.JLabel();
        jComboBoxAnimal = new javax.swing.JComboBox();
        jComboBoxTipo = new javax.swing.JComboBox();
        tfServico = new javax.swing.JTextField();
        btnAtualizar = new javax.swing.JButton();
        btnCancelar = new javax.swing.JButton();

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
        setTitle("Atualizar Agendamento");
        setResizable(false);

        panelComponents.setBorder(javax.swing.BorderFactory.createLineBorder(new java.awt.Color(0, 0, 0)));

        jlAnimal.setText("Animal:");

        jlTipo.setText("Tipo De Serviço:");

        jlTotal.setText("Total Serviço:");

        jComboBoxAnimal.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "Item 1", "Item 2", "Item 3", "Item 4" }));

        jComboBoxTipo.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "Item 1", "Item 2", "Item 3", "Item 4" }));

        btnAtualizar.setText("Adicionar");
        btnAtualizar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnAtualizarActionPerformed(evt);
            }
        });

        btnCancelar.setText("Cancelar");
        btnCancelar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnCancelarActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout panelComponentsLayout = new javax.swing.GroupLayout(panelComponents);
        panelComponents.setLayout(panelComponentsLayout);
        panelComponentsLayout.setHorizontalGroup(
            panelComponentsLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(panelComponentsLayout.createSequentialGroup()
                .addContainerGap()
                .addGroup(panelComponentsLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(panelComponentsLayout.createSequentialGroup()
                        .addComponent(jlAnimal)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addComponent(jComboBoxAnimal, 0, 350, Short.MAX_VALUE))
                    .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, panelComponentsLayout.createSequentialGroup()
                        .addComponent(jlTipo)
                        .addGap(18, 18, 18)
                        .addComponent(jComboBoxTipo, 0, 121, Short.MAX_VALUE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addGroup(panelComponentsLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                            .addGroup(panelComponentsLayout.createSequentialGroup()
                                .addGap(10, 10, 10)
                                .addComponent(btnAtualizar)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                .addComponent(btnCancelar))
                            .addGroup(panelComponentsLayout.createSequentialGroup()
                                .addComponent(jlTotal)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                .addComponent(tfServico)))))
                .addGap(33, 33, 33))
        );
        panelComponentsLayout.setVerticalGroup(
            panelComponentsLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(panelComponentsLayout.createSequentialGroup()
                .addGap(27, 27, 27)
                .addGroup(panelComponentsLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jlAnimal)
                    .addComponent(jComboBoxAnimal, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(18, 18, 18)
                .addGroup(panelComponentsLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jlTipo)
                    .addComponent(jlTotal)
                    .addComponent(jComboBoxTipo, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(tfServico, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(panelComponentsLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(btnAtualizar)
                    .addComponent(btnCancelar))
                .addContainerGap(15, Short.MAX_VALUE))
        );

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(panelComponents, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(panelComponents, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );

        java.awt.Dimension screenSize = java.awt.Toolkit.getDefaultToolkit().getScreenSize();
        setBounds((screenSize.width-476)/2, (screenSize.height-196)/2, 476, 196);
    }// </editor-fold>//GEN-END:initComponents

    private void btnAtualizarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnAtualizarActionPerformed
        int resp = JOptionPane.showConfirmDialog(this, "Confirma", "Confirma Atualização?", JOptionPane.YES_NO_OPTION, JOptionPane.QUESTION_MESSAGE);
        if (resp != 0) {
            return;
        }

        agenda.setIdAnimal((Animal) jComboBoxAnimal.getSelectedItem());
        agenda.setTipo((AgendaType) jComboBoxTipo.getSelectedItem());
        //SimpleDateFormat sdf = new SimpleDateFormat("dd/MM/yyyy");
        agenda.setDataAgendada(dataFormatada());
        agenda.setTotal(Float.parseFloat(tfServico.getText()));
        try {
            agDAO.atualizarAgenda(agenda);            
        } catch (SQLException ex) {
            ex.getMessage();
            JOptionPane.showConfirmDialog(this, "Erro ao Atualizar Agenda!");
            Logger.getLogger(">>>>>>: " + ex);
        }
        setVisible(false);
    }//GEN-LAST:event_btnAtualizarActionPerformed

    private Date dataFormatada() {
        Date d = new Date();
        SimpleDateFormat sdf = new SimpleDateFormat("dd/MM/yyyy");
        sdf.format(d);
        System.out.println("DATA>>>>>>>>>>: " + d);
        return d;

    }
    private void btnCancelarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnCancelarActionPerformed
        setVisible(false);
    }//GEN-LAST:event_btnCancelarActionPerformed
    /**
     * @param args the command line arguments
    
    public static void main(String args[]) {
    java.awt.EventQueue.invokeLater(new Runnable() {
    
    public void run() {
    frmAddAgenda dialog = new frmAddAgenda(new javax.swing.JFrame(), true);
    dialog.addWindowListener(new java.awt.event.WindowAdapter() {
    
    public void windowClosing(java.awt.event.WindowEvent e) {
    System.exit(0);
    }
    });
    dialog.setVisible(true);
    }
    });
    }*/
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton btnAtualizar;
    private javax.swing.JButton btnCancelar;
    private javax.swing.JComboBox jComboBoxAnimal;
    private javax.swing.JComboBox jComboBoxTipo;
    private javax.swing.JLabel jlAnimal;
    private javax.swing.JLabel jlTipo;
    private javax.swing.JLabel jlTotal;
    private javax.swing.JPanel panelComponents;
    private javax.swing.JTextField tfServico;
    // End of variables declaration//GEN-END:variables

    private void loadInitialComboData() {
        try {
            AnimalDAO anDAO = new AnimalDAO();
            DefaultComboBoxModel cbm = new DefaultComboBoxModel(AgendaType.values());
            jComboBoxTipo.setModel(cbm);
            Vector<Animal> animais = new Vector<Animal>(anDAO.getTodosAnimais());
            DefaultComboBoxModel cbm2 = new DefaultComboBoxModel(animais);
            jComboBoxAnimal.setModel(cbm2);
            /*DefaultComboBoxModel cbm2 = new DefaultComboBoxModel(AnimalType.values());
            jComboBoxAnimal.setModel(cbm2);*/
        } catch (Exception e) {
            e.printStackTrace();
            JOptionPane.showMessageDialog(this, "Erro ao carregar a lista!");
            return;
        }

    }

    private void loadAgendamentoInComboBox() {
        jComboBoxAnimal.setSelectedItem(agenda.getIdAnimal());
        jComboBoxAnimal.setSelectedItem(agenda.getTipo());
        tfServico.setText(String.valueOf(agenda.getTotal()));
    }
}
